import java.util.Stack;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


public class J15 {
	static Display display;
	static Shell shell;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		display = Display.getDefault();
		
		shell = new Shell(display);
		
		FormData layoutData = null;

		layoutData = new FormData();
		layoutData.left = new FormAttachment(0, 0);
		layoutData.top = new FormAttachment(0, 0);
		layoutData.right = new FormAttachment(100, 0);
		layoutData.bottom = new FormAttachment(100 , 0);
		
		final Canvas c = new Canvas(shell , SWT.DOUBLE_BUFFERED | SWT.BACKGROUND);
		
		c.addPaintListener(new PaintListener(){

			public void paintControl(PaintEvent e) {
				// TODO Auto-generated method stub
				GC gc = e.gc;
				
				gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
				
				final Point size = c.getSize();
//				gc.fillRectangle(0 , 0 , c.get);
			}
			
		});
		
		shell.setLayout(new FormLayout());
		
		c.setLayoutData(layoutData);
		
		final int numRows ,numCols;
		
		numRows = 21;
		numCols = 21;
		
		class Node{
			public int x, y;
			public Node(int x, int y){
				this.x = x;
				this.y = y;
			}
			
			public boolean isRightExplored;
			public boolean isBottomExplored;
			
			public boolean hasRightNode(){
				return x < numCols - 1;
			}
			
			public boolean hasBottomNode(){
				return y < numRows - 1;
			}
		}
		
		Node[][]grid = new Node[numRows][numCols];
		
		for (int i = 0; i < numRows; i++) {
			for (int j = 0; j < numCols; j++) {
				Node node = new Node(j , i);
				grid[i][j] = node;
			}
		}
		
		Node start = grid[0][0];
		Node end = grid[numRows - 1][numCols - 1];
		Stack<Node> stack = new Stack<Node>();
		
		stack.push(start);
		
		long numPath = 0;
		
		while(!stack.isEmpty()){
			Node top = stack.peek();
			
			if(top.x == end.x && top.y == end.y){
				stack.pop();
				numPath ++;
//				System.out.println("number of path [" + numPath + "]");
				continue;
			}
			
			if(top.hasRightNode()){
				if(!top.isRightExplored){
					top.isRightExplored = true;
					stack.push(grid[top.y][top.x + 1]);
					continue;
				}
			}
			
			if(top.hasBottomNode()){
				if(!top.isBottomExplored){
					top.isBottomExplored = true;
					stack.push(grid[top.y + 1][top.x]);
					continue;
				}
			}
			
			top = stack.pop();
			top.isRightExplored = false;
			top.isBottomExplored = false;
			
			display.asyncExec(new Runnable(){

				public void run() {
					// TODO Auto-generated method stub
					c.redraw();
				}
				
			});
		}
		
		
		
		System.out.println("number of paths [" + numPath + "]");
	}

}
